/// @Author: 初然
/// @Date: 2022-02-09
/// @Description: 权限检测工具类

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:permission_handler/permission_handler.dart';
import '../base_widget/permission_dialog.dart';

class PermissionUtils {
  static Future<bool> _requestPermission(Permission permission,
      {required String content ,required BuildContext context,required Function function}) async {
    var status = await permission.status;
    if (status.isDenied) {
      status = await permission.request();
    }
    if (status.isDenied || status.isPermanentlyDenied) {
      SPDialog.doubleItem(
          title: '没有权限',
          content: content,
          confirm: '去设置',
          onConfirm: () {
            openAppSettings();
          }).showAlert(context: context);
      function(false);
      return false;
    }
    function(true);
    return true;
  }

  /// 申请定位权限
  static  requestLocationPermission( BuildContext context,Function function) async {
    var status = await Permission.location.status;
    if (Platform.isAndroid && !status.isGranted) {
      SPDialog.doubleItem(
          title: '捷途申请位置权限目的说明',
          content: '位置权限仅用于发布动态、选择/查看经销商、查看附近权益或服务等需要定位权限的功能。',
          confirm: '去允许',
          onConfirm: ()  {
            _requestPermission(Permission.location,
                content: '需要使用地理位置，判断您当前位置或为您导航路线',context: context,function: function);

          }).showAlert(context: context);

    }else{
       _requestPermission(Permission.location,
          content: '需要使用地理位置，判断您当前位置或为您导航路线',context: context,function: function);
    }

  }

  static String hintText(String text) => '请在设置中允许$text权限';

  /// 申请相册保存权限
  static  requestPhotoSavePermission(BuildContext context,Function function) async {
    var status = await Permission.storage.status;
    if (Platform.isAndroid && !status.isGranted) {
      SPDialog.doubleItem(
          title: '捷途申请存储权限目的说明',
          content: '存储权限仅用于发布动态、用户聊天、修改头像、存海报、下载APP包等需要保存图片视频或文件的功能。',
          confirm: '去允许',
          onConfirm: () {
            _requestPermission(Permission.storage,
                content: '需要使用相册，上传图片或保存图片至本地',context: context,function:function );
          }).showAlert(context: context);

    }else{
       _requestPermission(
          Platform.isAndroid ? Permission.storage : Permission.photosAddOnly,
          content: '需要使用相册，上传图片或保存图片至本地',context: context,function: function);
    }
  }

  /// 申请相册读取权限
  static  requestPhotoPickPermission(BuildContext context,Function function) async {
    var status = await Permission.storage.status;
    if (Platform.isAndroid && !status.isGranted) {
      SPDialog.doubleItem(
          title: '捷途申请相册权限目的说明',
          content: '相册权限仅用于发布动态、商城晒图、用户聊天等需要获取图片或视频的功能。',
          confirm: '去允许',
          onConfirm: () {
            _requestPermission(Permission.storage,
                content: '需要使用相册，上传图片或保存图片至本地',context: context,function: function);
          }).showAlert(context: context);

    }else{
      _requestPermission(
          Permission.storage,
          content: '需要使用相册，上传图片或保存图片至本地',context: context,function: function);
    }
  }

  /// 申请相机权限
  static requestCameraPermission(BuildContext context,Function function) async {
    var status = await Permission.camera.status;
    if (Platform.isAndroid && !status.isGranted) {
      SPDialog.doubleItem(
          title: '捷途申请相机权限目的说明',
          content: '相机权限仅用于扫一扫、发布动态、商城晒图、用户聊天等需要拍摄照片或视频的功能。',
          confirm: '去允许',
          onConfirm: () {
            _requestPermission(Permission.camera, content: '需要使用相机扫一扫或拍摄图片',context: context,function: function);
          }).showAlert(context: context);

    }else{
       _requestPermission(Permission.camera, content: '需要使用相机扫一扫或拍摄图片',context: context,function: function);

    }
  }

  /// 申请麦克风权限
  static requestMicrophonePermission(BuildContext context,Function function) async {
    var status = await Permission.microphone.status;
    if (Platform.isAndroid && !status.isGranted) {
      SPDialog.doubleItem(
          title: '捷途申请麦克风权限目的说明',
          content: '麦克风权限仅用于仅用于用户聊天、发布动态、权益打卡等需要录制音频的功能。',
          confirm: '去允许',
          onConfirm: () {
            _requestPermission(Permission.microphone,
                content: '需要使用麦克风聊天或使得录制的视频有声音',context: context,function: function);
          }).showAlert(context: context);
    }else{
      _requestPermission(Permission.microphone,
          content: '需要使用麦克风聊天或使得录制的视频有声音',context: context,function: function);
    }
  }
}
